
/**
 * Namespace    Classes.Service
 * Class        OrderStates
 * Author       Bram Vandenbussche
 * Description  Provides service level and access level logic for OrderState objects
 * Version      1.0
**/

var OrderStatesService = new function() {
    var root = this;
    
    this.States = null;
    this.SavedFileName = "orderstates.xml";
    
    this.LoadOrderStates = function() {
		root.States = [];
	
        var xml = LoadXmlFromFile(this.SavedFileName);
        
        if (xml != undefined) {
			air.trace("OrderStatesService: File found! Loading file xml");
			
            // if there is, load the settings from it
            this.LoadOrderStatesFromFile(xml);

            // and display them
            this.RenderOrderStates();            
        } else {
			air.trace("OrderStatesService: No file found. Loading from WS")
			
            // if there isn't, create the default settings
            this.LoadOrderStatesFromWS(this.SaveAndRender);
        }
    }
    
    
    this.SaveAndRender = function() {
        // Order staten opslaan naar xml
        root.PersistOrderStates();
        
        // Order staten view renderen
        root.RenderOrderStates();
    }
    
    
    this.LoadOrderStatesFromFile = function(xml) {
        $.each($("state", xml), function() {
            var o = new OrderState();
            
            // Setting aanmaken vanuit xml
            o.Deserialize($(this));
            
            // State toevoegen opslaan
            root.AddState(o);
        });
    }
    
    
    this.AddState = function(state) {
        this.States[state.ID] = state;
    }
    
    
    this.GetByID = function(_stateID) {
        return root.States[_stateID];
    }
    
    
    this.PersistOrderStates = function() {
        var xml = SerializeList(this.States, "states");
        
        SaveXmlToFile(xml, this.SavedFileName);
    }
    
    
    this.AssembleFromJson = function(sourceList) {
        for (var i = 0; i < sourceList.length; i++) {
            var oc = new OrderState(sourceList[i].id);
            
            oc.AssembleFromJson(sourceList[i]);
            
            root.States[oc.ID] = oc;
        }
    }
    
    
    this.LoadOrderStatesFromWS = function(callback) {
        var getOrderStatesUrl = PrestashopService.PS_MOBILE_AUTHORIZATION.api.order_states["xlink:href"] + "?display=full";
        
        var getOrderStatesArgs = {
            url: getOrderStatesUrl,
            method: 'GET',
            dataType: "xml",
            beforeSend: function(jqXHR, settings) {            
                // Authentication
                PrestashopService.PrepareAjaxCredentials(jqXHR);
            },
            success: function(data, textStatus, jqXHR) {
                // Lijst met order staten verwerken
                root.AssembleFromJson($.xml2json(data).order_states.order_state);
                
                // Callback functie aanroepen
                callback();
            },
            error: function(jqXHR, textStatus, errorThrown) {
                $("#debug").html(textStatus);
            }
        }
        
        $.ajax(getOrderStatesArgs);
    }
    
    
    this.RenderOrderStates = function() {
    
    }
	
	
	this.ClearOrderStates = function() {
		root.States = null;
	}
	
	
	this.ClearCache = function() {
		// Clear orderstates file
		DeleteFile(root.SavedFileName);
		
		// Clear order states in memory
		root.ClearOrderStates();
		
		// Order states opnieuw ophalen
		root.LoadOrderStates();
	}
}